package com.srpingde.hhh;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import com.springde.hhh.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.XiangQing;
import com.srpingde.hhh.bean.XiangQingData;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class XiangQingActivity extends Activity {
	ImageView back,show_iv;
	HHHIml weiQuIml;
	LinearLayout baobei_xiangqing_ly;
	TextView xiaoliang,shichangjia,weiqujia,name,user_shoucangjia,num,chan;
	XiangQing xiangQing;
	LinearLayout leixing_rl;
	ListView kuanshi_item_ly;
	Button kuanshi,shoucang , gowucheBtn;
	String id = null,image = null,jiage;
	String kuanshi_select;
	int shownum = 1;
	String strs[];
	boolean check;
	Boolean hideShoucang;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiang_qing);
        kuanshi_select = null;
        back = (ImageView) findViewById(R.id.back);
        show_iv = (ImageView) findViewById(R.id.xiangqing_iv);
        weiQuIml = new HHHIml();
        baobei_xiangqing_ly = (LinearLayout) findViewById(R.id.baobei_xiangqing);
        xiaoliang = (TextView) findViewById(R.id.xiaoliang);
        chan = (TextView) findViewById(R.id.chan);
        shichangjia = (TextView) findViewById(R.id.shichangjia);
        weiqujia = (TextView) findViewById(R.id.weiqujia);
        name = (TextView) findViewById(R.id.name);
        user_shoucangjia = (TextView) findViewById(R.id.user_shoucangjia);
        //desc = (TextView) findViewById(R.id.desc);
        leixing_rl = (LinearLayout) findViewById(R.id.relativelayout_kuanshi);
        kuanshi_item_ly = (ListView) findViewById(R.id.kuanshi_item_ly);
        shoucang = (Button) findViewById(R.id.xiangxi_zaixian);
        gowucheBtn = (Button) findViewById(R.id.xiangxi_gouweuche);
        kuanshi = (Button) findViewById(R.id.kuanshi_btn);
        kuanshi.setVisibility(View.GONE);
        chan.setVisibility(View.GONE);
        num = (TextView) findViewById(R.id.num);
        layout();
        id = getIntent().getExtras().getString("id");
    	hideShoucang = getIntent().getBooleanExtra("hideShoucang", false);
    	if( hideShoucang == true ){
    		shoucang.setVisibility(View.GONE);
    	}
    	
        Log.e("mytag",id+"======================0000000000000000000000000000000000000得到的详情的id");
        if(getIntent().getExtras()!=null){
        	getData(HHHInterface.httpHeader + "api.php?tab=products&act=data&pid="+id+"&uid="+HhhApp.getInstance().getUid());
        }
        kuanshi_item_ly.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				kuanshi_select = strs[arg2];
				check = false;
			}
		});
    }
    public void onClick(View v) throws UnsupportedEncodingException{
    	switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.kuanshi_btn:
			leixing_rl.setVisibility(View.VISIBLE);
			kuanshi.setVisibility(View.GONE);
			chan.setVisibility(View.GONE);
			break;
		case R.id.kuanshi_back:
			leixing_rl.setVisibility(View.GONE);
			kuanshi.setVisibility(View.VISIBLE);
			chan.setVisibility(View.VISIBLE);
			if(check==false){
				if(kuanshi_select!=null){
					kuanshi.setText(kuanshi_select);
				}
			}
			break;
		case R.id.xiangxi_gouweuche:
//			WeiQuDB weiQuDB = new WeiQuDB(XiangQingActivity.this);
//			long check = weiQuDB.insert(id, image, jiage,kuanshi_select);
//			Log.e("mytag", check+"=====================");
//			if(check!=0){
//				WeiQuApp.getInstance().showDialog(XiangQingActivity.this, "加入购物车成功");
//			}
			if(!HhhApp.getInstance().getUname().equals("")){
				if(kuanshi_select==null || kuanshi_select.equals("")){
					HhhApp.getInstance().showDialog(XiangQingActivity.this, "请选择款式");
				}else{
					addToGouWuChe();
				}
			}else{
				HhhApp.getInstance().showDialog(XiangQingActivity.this, "请先登录");
			}
			break;
		case R.id.xiangxi_zaixian:
			Log.e("mytag", HhhApp.getInstance().getUid()+"================收藏返回的数据");
			if(!HhhApp.getInstance().getUname().equals("")){
				collection();
			}else{
				HhhApp.getInstance().showDialog(XiangQingActivity.this, "请先登录");
			}
			break;
		case R.id.add:
			shownum++;
			num.setText(String.valueOf(shownum));
			break;
		case R.id.jian:
			shownum--;
			if(shownum<=0){
				shownum = 1;
			}
			num.setText(String.valueOf(shownum));
			break;
		}
    }
    private void getData(String str){
    	weiQuIml.fenLeiItemXiangQing(XiangQingActivity.this, str,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				xiangQing = HHHDataManage.getInstance().getXiangQing();	
					for(XiangQingData xiangQingData : xiangQing.getRows()){
						final ImageView kvShow = show_iv ;		
			    		ImageLoader.getInstance().loadImage(HHHIml.httpHeader+xiangQingData.getUrl(), new SimpleImageLoadingListener(){
			    			@Override
			    			public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
			    				BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
			    				//kvShow.setImageBitmap(bitmap);
			    				kvShow.setBackgroundDrawable(bitmapDrawable);
			    			}
			    		});	
						shichangjia.setText(xiangQingData.getMarket());
						//desc.setText(desc.getText()+" : "+xiangQingData.getDesc());
						name.setText(name.getText()+" : "+xiangQingData.getName());
						weiqujia.setText(weiqujia.getText()+xiangQingData.getPrice());
						strs = new String[xiangQingData.getStyle().size()];
						for(int j = 0;j<xiangQingData.getStyle().size();j++){
//							View view = getLayoutInflater().from(XiangQingActivity.this).inflate(R.layout.xiangqing_leixing_item, null);
//				            final TextView textView = (TextView) view.findViewById(R.id.show_item_kuanshi);
//				        	textView.setText(xiangQingData.getStyle().get(j).getDesc());
//				        	kuanshi_item_ly.addView(view);
//				        	textView.setOnClickListener(new OnClickListener() {
//								
//								@Override
//								public void onClick(View v) {
//									// TODO Auto-generated method stub
//									kuanshi_select = textView.getText().toString();
//								}
//							});
							strs[j] = xiangQingData.getStyle().get(j).getDesc();
						}
						ArrayAdapter<String> adapter = new ArrayAdapter<String>(XiangQingActivity.this, R.layout.xiangqing_leixing_item,R.id.show_item_kuanshi,strs);
						kuanshi_item_ly.setAdapter(adapter);
			    		xiaoliang.setText(xiaoliang.getText()+xiangQingData.getmonthAmount()+"件");	
						for(int i = 0;i<xiangQingData.getOtherpic().size();i++){
							ImageView imageView = new ImageView(XiangQingActivity.this);
							LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, HhhApp.getInstance().getDisplayHightAndWightPx()[1]);
							imageView.setLayoutParams(layoutParams);
							final ImageView shouw = imageView ;
					    	ImageLoader.getInstance().loadImage(HHHIml.httpHeader+xiangQingData.getOtherpic().get(i).getUrl(), new SimpleImageLoadingListener(){
					    		@Override
					    		public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
					    			BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
					    			//kvShow.setImageBitmap(bitmap);
					    			shouw.setBackgroundDrawable(bitmapDrawable);
					    		}
					    	});	
							baobei_xiangqing_ly.addView(imageView);
						}
				}
				//listBeans = WeiQuDataManage.getInstance().getGuangGaos();
				//fenlei_name_gridview.setAdapter(new FenLeiListViewAdapter(FenLeiNameActivity.this,listBeans));
			}
		});
    }
    private void collection(){
    	String url = HHHInterface.httpHeader + "api.php?tab=favorite&act=add&uid="+HhhApp.getInstance().getUid()+"&type=1&pid="+id;
    	weiQuIml.loginHttp(XiangQingActivity.this,url, 
    			new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				Log.e("mytag", content+"================收藏返回的数据");
				try {
					JSONObject jsonObject = new JSONObject(content);
					if(jsonObject.getBoolean("return")){
						HhhApp.getInstance().showDialog(XiangQingActivity.this, "收藏成功");
					}else{
						HhhApp.getInstance().showDialog(XiangQingActivity.this, "已经收藏过了");
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});	
    }
    private void addToGouWuChe() throws UnsupportedEncodingException{
    	String url = HHHInterface.httpHeader + "api.php?tab=cart&act=add&pid=" + id + "&amount=" + num.getText().toString() + "&uid=" + HhhApp.getInstance().getUid() + "&style="+ URLEncoder.encode(kuanshi_select, "utf-8");
    	Log.e("mytag", url+"=========款式");
    	weiQuIml.loginHttp(XiangQingActivity.this,url, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				Log.e("mytag", content+"================收藏返回的数据");
				try {
					JSONObject jsonObject = new JSONObject(content);
					if(jsonObject.getBoolean("return")){
						HhhApp.getInstance().showDialog(XiangQingActivity.this, "添加成功");
					}else if(!jsonObject.getBoolean("return")){
						HhhApp.getInstance().showDialog(XiangQingActivity.this, "已经添加了");
					}else{
						HhhApp.getInstance().showDialog(XiangQingActivity.this, "添加失败");
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});	
    }
    private void layout() {
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		//back
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
		
		btnLayout((Button)findViewById(R.id.xiangxi_zaixian));
		btnLayout((Button)findViewById(R.id.xiangxi_gouweuche));
		
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xiangqing_kuanshi);
		LinearLayout.LayoutParams kuanshiLayoutParams = (android.widget.LinearLayout.LayoutParams) kuanshi.getLayoutParams();
		kuanshiLayoutParams.height = bitmap.getHeight()/2;
		kuanshi.setLayoutParams(kuanshiLayoutParams);
		
	}
    private void btnLayout(Button button){
    	LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
    	layoutParams.width = HhhApp.getInstance().getDisplayHightAndWightPx()[1]/2;
    	layoutParams.height =  HhhApp.getInstance().title_h();
    	button.setLayoutParams(layoutParams);
    	
    	RelativeLayout.LayoutParams imageLayoutParams = (LayoutParams) show_iv.getLayoutParams();
    	imageLayoutParams.height = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.5);
    	show_iv.setLayoutParams(imageLayoutParams);
    }
}
